/*
* TransitionController.java
* Created on  2015/7/27 15:06
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.tools;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.tool.Transition;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息YY
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/tools/transition")
public class TransitionController {

    @Autowired
    private ToolExpressionService toolExpressionService;

    @Autowired
    private SessionService sessionService;
    @RequestMapping("")
    public String getPage(){
        sessionService.setEnvironment('T');
        return "tools/transition";
    }

    @RequestMapping(value = "/updateParam",method = RequestMethod.POST)
    @ResponseBody
    public void updateParam(Transition transition) {
        toolExpressionService.updateToolsParam(transition);
    }

    @RequestMapping(value = "/clean",method = RequestMethod.POST)
    @ResponseBody
    public void clean() {
        toolExpressionService.clearMap();
        toolExpressionService.clearResultMap();
    }

    @RequestMapping(value = "/calE15",method = RequestMethod.GET)
    @ResponseBody
    public Double calE15() {
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E4 = paramMap.get("TE4");
        Double E4S= paramMap.get("TE4");
        Double E15;
        if (E4 == 3)
            E4 = 1.0;
        paramMap.put("TE4",E4);
        E15 = toolExpressionService.calculate("TE15");
        paramMap.put("TE4",E4S);
        return E15;
    }

    @RequestMapping(value = "/cal", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Double> calAll() {
        Map<String, Double> map = new HashMap<>();
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E4 = paramMap.get("TE4");
        paramMap.put("TE4",E4);
        Double E93 = null;
        Double E9= paramMap.get("TE9");
        Double E98= E9;
        paramMap.put("TE98",E98);
        E93 = E4;
        if (E4 == 2)
            E93 = 2.0;
        if (E4 == 3)
            E93 = 2.0;
        if (E4 == 4)
            E93 = 1.0;
        paramMap.put("TE93",E93);
        toolExpressionService.calculate("TE14");
        if (E4==1 ){
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE18");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE97");
            toolExpressionService.calculate("TE99");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE106");
            toolExpressionService.calculate("TE108");
            toolExpressionService.calculate("TE109");
            toolExpressionService.calculate("TE110");
            toolExpressionService.calculate("TE111");
            toolExpressionService.calculate("TE112");
            toolExpressionService.calculate("TE113");
            toolExpressionService.calculate("TE114");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e1s",toolExpressionService.calculate("TE115"));
            map.put("e2s",toolExpressionService.calculate("TE108"));
            map.put("e3s",toolExpressionService.calculate("TE116"));
        }else if (E4 == 2){
            Double E6 = paramMap.get("TE6");
            Double E5 = paramMap.get("TE5");
            Double E94 = paramMap.get("TE94");
            Double E95 = paramMap.get("TE95");
            if (E6.equals(E95) || E5 .equals(E94)){
                toolExpressionService.calculate("TE8");
                toolExpressionService.calculate("TE16");
                toolExpressionService.calculate("TE17");
                toolExpressionService.calculate("TE19");
                toolExpressionService.calculate("TE20");
                toolExpressionService.calculate("TE21");
                toolExpressionService.calculate("TE22");
                toolExpressionService.calculate("TE23");
                toolExpressionService.calculate("TE24");
                toolExpressionService.calculate("TE100");
                toolExpressionService.calculate("TE101");
                toolExpressionService.calculate("TE103");
                toolExpressionService.calculate("TE104");
                toolExpressionService.calculate("TE105");
                toolExpressionService.calculate("TE118");
                toolExpressionService.calculate("TE119");
                toolExpressionService.calculate("TE130");
                toolExpressionService.calculate("TE131");
                toolExpressionService.calculate("TE132");
                toolExpressionService.calculate("TE133");
                toolExpressionService.calculate("TE134");
                toolExpressionService.calculate("TE135");
                toolExpressionService.calculate("TE136");
                toolExpressionService.calculate("TE137");
                toolExpressionService.calculate("TE138");
                toolExpressionService.calculate("TE139");
                map.put("e21",toolExpressionService.calculate("TE21"));
                map.put("e19",toolExpressionService.calculate("TE19"));
                map.put("e106",toolExpressionService.calculate("TE106"));
                map.put("e2s",toolExpressionService.calculate("TE132"));
                map.put("e1s",toolExpressionService.calculate("TE140"));
                map.put("e3s",toolExpressionService.calculate("TE141"));
                }else {
                toolExpressionService.calculate("TE8");
                Double E15 = paramMap.get("TE15");
                map.put("TE15",E15);
                toolExpressionService.calculate("TE16");
                toolExpressionService.calculate("TE17");
                toolExpressionService.calculate("TE19");
                toolExpressionService.calculate("TE20");
                toolExpressionService.calculate("TE21");
                toolExpressionService.calculate("TE22");
                toolExpressionService.calculate("TE23");
                toolExpressionService.calculate("TE24");
                toolExpressionService.calculate("TE100");
                toolExpressionService.calculate("TE101");
                toolExpressionService.calculate("TE103");
                toolExpressionService.calculate("TE104");
                toolExpressionService.calculate("TE105");
                toolExpressionService.calculate("TE118");
                toolExpressionService.calculate("TE119");
                toolExpressionService.calculate("TE120");
                toolExpressionService.calculate("TE121");
                toolExpressionService.calculate("TE122");
                toolExpressionService.calculate("TE123");
                toolExpressionService.calculate("TE124");
                toolExpressionService.calculate("TE125");
                toolExpressionService.calculate("TE126");
                map.put("e21",toolExpressionService.calculate("TE21"));
                map.put("e19",toolExpressionService.calculate("TE19"));
                map.put("e106",toolExpressionService.calculate("TE106"));
                map.put("e2s",toolExpressionService.calculate("TE120"));
                map.put("e1s",toolExpressionService.calculate("TE127"));
                map.put("e3s",toolExpressionService.calculate("TE128"));
            }
        }else if (E4==3 ){
            paramMap.put("TE4",1.0);
            toolExpressionService.calculate("TE8");
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE18");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE143");
            toolExpressionService.calculate("TE144");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE145");
            toolExpressionService.calculate("TE146");
            toolExpressionService.calculate("TE147");
            toolExpressionService.calculate("TE148");
            toolExpressionService.calculate("TE149");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e2s",toolExpressionService.calculate("TE143"));
            map.put("e1s",toolExpressionService.calculate("TE150"));
            map.put("e3s",toolExpressionService.calculate("TE151"));
        }else{
            paramMap.put("TE4",2.0);
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE153");
            toolExpressionService.calculate("TE154");
            toolExpressionService.calculate("TE155");
            toolExpressionService.calculate("TE156");
            toolExpressionService.calculate("TE157");
            toolExpressionService.calculate("TE158");
            toolExpressionService.calculate("TE159");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e2s",toolExpressionService.calculate("TE153"));
            map.put("e1s",toolExpressionService.calculate("TE160"));
            map.put("e3s",toolExpressionService.calculate("TE161"));
        }
        toolExpressionService.getResult().put("p", map.get("e19"));
        toolExpressionService.getResult().put("z",map.get("e1s"));
        toolExpressionService.getResult().put("lost",map.get("e1s") * map.get("e19"));
        return map;
    }

    @RequestMapping(value = "transitionCal", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse transitionCal(@RequestBody Map<String, Double> paramMap) {
        try {
            return  toolExpressionService.transitionCal(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
